package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.user.UserAPI;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * Created by zghgchao 2019/8/5 9:30
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    // check : 启动检查服务是否开启，false不启动检查
    // loadbalance : 配置负载均衡策略
    @Reference(interfaceClass = UserAPI.class,check = false,loadbalance = "roundrobin")
    private UserAPI userAPI;

    @PostMapping("/register")
    public ResponseVO register(UserModel model){
        if (model.getUsername() == null || model.getUsername().trim().length() == 0){
            return ResponseVO.serviceFail("用户名不能为空！");
        }
        if (model.getPassword() == null || model.getPassword().trim().length() == 0){
            return ResponseVO.serviceFail("密码不能为空！");
        }
        boolean isSuccess = userAPI.register(model);
        if (isSuccess){
            return ResponseVO.success("注册成功！");
        }else {
            return ResponseVO.serviceFail("注册失败！");
        }
    }

    @GetMapping("/check")
    public ResponseVO check(String username){
        if (username == null || username.trim().length() == 0){
            return ResponseVO.serviceFail("用户名不能为空！");
        }
        // 当返回为true时，标识用户名可以
        boolean notExists = userAPI.checkUsername(username);
        if (notExists){
            return ResponseVO.success("用户名不存在！");
        }else {
            return ResponseVO.serviceFail("用户名已存在！");
        }
    }

    @GetMapping("/logout")
    public ResponseVO logout(String username){
        /**
         * 应用：
         *      1.前端存储JWT 【7天】：JWT的刷新
         *      2.服务器端会存储活动用户信息【30分钟】
         *      3.JWT里的userId为Key，查找活跃用户
         * 退出：
         *      1.前端删除掉JWT
         *      2.后端服务器删除活跃用户缓存
         * 现状：
         *      1.前端删除JWT
         */
        return ResponseVO.success("用户退出成功！");
    }

    @GetMapping("/getUserInfo")
    public ResponseVO getUserInfo(){
        // 获取当前登录用户
        String userId = CurrentUser.getCurrentUser();
        if (userId == null || userId.trim().length() == 0){
            return ResponseVO.serviceFail("用户未登录！");
        }

        int uuid = Integer.valueOf(userId);
        UserInfoModel userInfo = userAPI.getUserInfo(uuid);
        if (userInfo != null){
            return ResponseVO.success(userInfo);
        }else {
            return ResponseVO.serviceFail("用户信息查询失败！");
        }
    }

    @PostMapping(value = "/updateUserInfo")
    public ResponseVO updateUserInfo(UserInfoModel model){
        // 获取当前登录用户
            String userId = CurrentUser.getCurrentUser();
        if (userId == null || userId.trim().length() == 0){
            return ResponseVO.serviceFail("用户未登录！");
        }

        int uuid = Integer.valueOf(userId);
        // 判断当前登录人员的ID与修改的结果ID是否一致
        if (uuid != model.getUuid()){
            return ResponseVO.serviceFail("请修改您个人的信息！");
        }
        UserInfoModel userInfo = userAPI.updateUserInfo(model);
        if (userInfo != null){
            return ResponseVO.success(userInfo);
        }else {
            return ResponseVO.serviceFail("用户信息修改失败！");
        }
    }
}
